package com.chanyachting.emall.action.sale;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Sellyacht;
import com.chanyachting.emall.form.SellyachtForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.SellyachtService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.SellyachtServiceImpl;
import com.chanyachting.emall.util.PageUtil;

public class QuerySaleAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//一般页面记录数
		int COMMON_PAGE_SIZE = 20;
		
		SellyachtService syService = new SellyachtServiceImpl();
		SellyachtForm sf = (SellyachtForm)form;
		ChannelService cService = new ChannelServiceImpl();
		
		int currentPage = sf.getCurrentPage();
		int pageSize = COMMON_PAGE_SIZE;
		
		//取得subChannelList
		int channelId = sf.getChannelId();
		Channel c = cService.getOneChannel(new Channel(channelId));
		
		String channelName = sf.getChannelName();
		if (c == null) {
			return mapping.findForward("error");
		}
		channelName = c.getName();
		
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}
		
		String channelStr = String.valueOf(channelId);
		
		for(Channel tmpC:subChannelList) {
			channelStr = channelStr + "," + tmpC.getId();
		}
		
		Sellyacht sellyacht = null;
		String productPriceStr = "";
		String lengthStr = "";
		if (sf.getKeyword() != null && !"".equals(sf.getKeyword())) {
			sellyacht = new Sellyacht();
			int minLength = sf.getMinLength();
			int maxLength = sf.getMaxLength();
			if (minLength != 0 && maxLength != 0 && maxLength > minLength) {
				lengthStr = "between " + minLength + " and " + maxLength;
			}
			
			int minProductPrice = sf.getMinProductPrice();
			int maxProductPrice = sf.getMaxProductPrice();
			if (minProductPrice !=0 && maxProductPrice != 0 && maxProductPrice > minProductPrice) {
				productPriceStr = "between " + minProductPrice + " and " + maxProductPrice;
			}
			sellyacht.setTitle(sf.getKeyword());
			sellyacht.setCity(sf.getCity());
		}
		
		int pageCount = syService.QueryAllSellyachtCountByChannelId(channelStr)/pageSize;
		if (pageCount%pageSize > 0) {
			pageCount = pageCount + 1;
		}
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		List<Sellyacht> sy = syService.QueryAllSellyachtByChannelId(currentPage, pageSize,channelStr);
		sf.setSyList(sy);
		String toPageAct = PageUtil.getGotoAction(request.getRequestURL().toString(), request.getParameterMap());
		String linkStr = PageUtil.getFy3(pageCount, currentPage, toPageAct);
		sf.setLinkStr(linkStr);
		String selectionStr = PageUtil.getFy4(pageCount, currentPage, toPageAct);
		sf.setSelectionStr(selectionStr);
		sf.setChannelName(channelName);
		
		Map<String,Sellyacht> channelList = new TreeMap<String,Sellyacht>();
		
		for(int i=0;i<sy.size();i++) {
			channelList.put(PageUtil.getSubChannelName(subChannelList,sy.get(i).getChannelId()) + "&" + i, sy.get(i));
		}
		
		sf.setSubChannelList(subChannelList);
		sf.setChannelList(channelList);
		sf.setSubChannelId("");
		
		return mapping.findForward("toAllSellyacht");
	}
}
